package com.wgchao.identityrightsmanagement.application.controller;

import com.wgchao.identityrightsmanagement.application.service.AccountApplicationService;
import com.wgchao.identityrightsmanagement.domain.organization.model.AccountCreator;
import com.wgchao.identityrightsmanagement.domain.organization.model.AccountUpdator;
import com.wgchao.identityrightsmanagement.infrastructure.utils.PageData;
import com.wgchao.identityrightsmanagement.infrastructure.utils.ResponseData;
import com.wgchao.identityrightsmanagement.query.AccountQuery;
import com.wgchao.identityrightsmanagement.query.pageparam.AccountPageParam;
import com.wgchao.identityrightsmanagement.query.vo.AccountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @ClassName: AccountController
 * @author: wgchao
 * @createTime: 2020/6/15 3:59 PM
 * @Description: 管理员账户管理
 */
@RestController
@RequestMapping("/sntk/sys/base/account")
@Api(value = "AccountController", description = "管理员账户管理")
public class AccountController {
    @Autowired
    private AccountApplicationService accountApplicationService;
    @Autowired
    private AccountQuery accountQuery;


    @ApiOperation(value = "向机构添加管理员账户")
    @PostMapping("/{agencyCode}")
    public ResponseData addAccountToAgency(@ApiParam(name = "agencyCode", value = "机构编号", required = true) @PathVariable String agencyCode,
                                           @ApiParam(name = "creator", value = "账户信息", required = true) @RequestBody AccountCreator creator){
        accountApplicationService.addAccount(agencyCode, creator, "admin");
        return ResponseData.success();
    }

    @ApiOperation(value = "修改账户基本信息")
    @PutMapping("/{username}")
    public ResponseData modifyAccountInfo(@ApiParam(name = "username", value = "账户名", required = true) @PathVariable String username,
                                          @ApiParam(name = "updator", value = "账户信息", required = true) @RequestBody AccountUpdator updator){
        accountApplicationService.modifyAccountInfo(username, updator, "admin");
        return ResponseData.success();
    }

    @ApiOperation(value = "禁用账户")
    @PutMapping("/disabled/{username}")
    public ResponseData disabled(@ApiParam(name = "username", value = "账户名", required = true) @PathVariable String username){
        accountApplicationService.disable(username, "admin");
        return ResponseData.success();
    }

    @ApiOperation(value = "启用账户")
    @PutMapping("/regain/{username}")
    public ResponseData regain(@ApiParam(name = "username", value = "账户名", required = true) @PathVariable String username){
        accountApplicationService.regain(username, "admin");
        return ResponseData.success();
    }

    @ApiOperation(value = "删除账户")
    @DeleteMapping("/{username}")
    public ResponseData remove(@ApiParam(name = "username", value = "账户名", required = true) @PathVariable String username){
        accountApplicationService.remove(username, "admin");
        return ResponseData.success();
    }

    @ApiOperation(value = "批量删除账户")
    @DeleteMapping
    public ResponseData remove(@ApiParam(name = "usernames", value = "账户名列表", required = true) @RequestBody List<String> usernames){
        accountApplicationService.removeAll(usernames, "admin");
        return ResponseData.success();
    }

    @ApiOperation(value = "为账户分配部门")
    @PostMapping("/assign/{username}/to/{departmentCode}")
    public ResponseData assignAccountToDepartment(@ApiParam(name = "username", value = "账户名", required = true) @PathVariable String username,
                                                  @ApiParam(name = "departmentCode", value = "部门编号", required = true) @PathVariable String departmentCode){
        accountApplicationService.assignAccountToDepartment(username, departmentCode, "admin");
        return ResponseData.success();
    }

    @ApiOperation(value = "为账户分配职位")
    @PostMapping("/assign/office/to/{username}")
    public ResponseData assignOfficesToAccount(@ApiParam(name = "username", value = "账户名", required = true) @PathVariable String username,
                                                  @ApiParam(name = "officeCodes", value = "职位编号", required = true) @RequestBody List<String> officeCodes){
        accountApplicationService.assignOfficesToAccount(username, officeCodes, "admin");
        return ResponseData.success();
    }

    @ApiOperation(value = "查询账户信息")
    @GetMapping("/{username}")
    public ResponseData<AccountVo> findByUserName(@ApiParam(name = "username", value = "账户名", required = true) @PathVariable String username) {
        AccountVo accountVo = accountQuery.findByUserName(username);
        return ResponseData.success().setData(accountVo);
    }

    @ApiOperation(value = "条件分页查询账户信息")
    @GetMapping("/page/condition")
    public ResponseData<PageData<AccountVo>> findByConditionsOfPage(@ApiParam(name = "accountPageParam", value = "条件", required = true)
                                                                        AccountPageParam accountPageParam) {
        PageData<AccountVo> accountVoPageData = accountQuery.findByConditionsOfPage(accountPageParam);
        return ResponseData.success().setData(accountVoPageData);
    }
}
